package mosync.mobilelualiveditor.ui;

import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

public class WidgetType
{
	public WidgetType(String name)
	{
		this.mName = name;
		// UNUSED: this.base = null;
		this.mPropertyTypes = new ArrayList<WidgetPropertyType>();
		// UNUSED: types.put(name, this);
	}

	public WidgetType(String name, WidgetType base)
	{
		this.mName = name;
		// UNUSED: this.base = base;
		this.mPropertyTypes = new ArrayList<WidgetPropertyType>();
		// UNUSED: types.put(name, this);

		ArrayList<WidgetPropertyType> pTypes = base.getPropertyTypes();
		for (WidgetPropertyType pType : pTypes)
		{
			mPropertyTypes.add(pType);
		}
	}

	public String getName()
	{
		return mName;
	}

	public void addProperty(WidgetPropertyType property)
	{
		mPropertyTypes.add(property);
	}

	public ArrayList<WidgetPropertyType> getPropertyTypes()
	{
		return mPropertyTypes;
	}

	public static void addType(WidgetType type)
	{
		mTypes.put(type.getName(), type);
	}

	public static WidgetType[] getTypes()
	{
		WidgetType[] widgetTypes = new WidgetType[mTypes.size()];
		int i = 0;
		for (WidgetType type : mTypes.values())
		{
			widgetTypes[i++] = type;
		}
		return widgetTypes;
	}

	public static void initTypes()
	{
		WidgetType widgetType = new WidgetType("Widget");
		widgetType.addProperty(new WidgetPropertyType("int", "width", "-2"));
		widgetType.addProperty(new WidgetPropertyType("int", "height", "-2"));
		widgetType.addProperty(new WidgetPropertyType("int", "left", "0"));
		widgetType.addProperty(new WidgetPropertyType("int", "top", "0"));
		widgetType.addProperty(new WidgetPropertyType("float", "alpha", "1.0"));
		widgetType.addProperty(new WidgetPropertyType("color",
			"backgroundColor", "00000000"));

		WidgetType screenType = new WidgetType("Screen");
		screenType.addProperty(new WidgetPropertyType("string", "title", ""));
		screenType.addProperty(new WidgetPropertyType("resource", "icon", "0"));

		WidgetType tabScreenType = new WidgetType("TabScreen", screenType);
		tabScreenType.addProperty(new WidgetPropertyType("int", "currentTab",
			"0"));

		WidgetType navScreenType = new WidgetType("NavScreen", screenType);

		WidgetType buttonType = new WidgetType("Button", widgetType);
		buttonType.addProperty(new WidgetPropertyType("string", "text", ""));
		buttonType.addProperty(new WidgetPropertyType("resource",
			"backgroundImage", "0"));
		buttonType.addProperty(new WidgetPropertyType("float", "fontSize",
			"12.0"));
		buttonType.addProperty(new WidgetPropertyType("color", "fontColor",
			"ff000000"));

		WidgetType imageButtonType = new WidgetType("ImageButton", buttonType);

		WidgetType imageType = new WidgetType("Image", widgetType);
		imageType.addProperty(new WidgetPropertyType("resource", "image", "0"));
		imageType
			.addProperty(new WidgetPropertyType("int", "leftCapWidth", "0"));
		imageType
			.addProperty(new WidgetPropertyType("int", "topCapWidth", "0"));

		WidgetType labelType = new WidgetType("Label", widgetType);
		labelType.addProperty(new WidgetPropertyType("string", "text", ""));
		labelType.addProperty(new WidgetPropertyType("float", "fontSize",
			"12.0"));
		labelType.addProperty(new WidgetPropertyType("color", "fontColor",
			"ff000000"));
		labelType.addProperty(new WidgetPropertyType("enum",
			"verticalAlignment", "center"));
		labelType.addProperty(new WidgetPropertyType("enum",
			"horizontalAlignment", "center"));

		// must add more properties here (enums etc).

		WidgetType editboxType = new WidgetType("EditBox", widgetType);
		editboxType.addProperty(new WidgetPropertyType("string", "text", ""));
		editboxType.addProperty(new WidgetPropertyType("string", "placeholder",
			""));
		editboxType.addProperty(new WidgetPropertyType("bool",
			"autocorrectionEnabled", "false"));
		editboxType.addProperty(new WidgetPropertyType("bool", "showKeyboard",
			"false"));
		editboxType.addProperty(new WidgetPropertyType("float", "fontSize",
			"12.0"));
		editboxType.addProperty(new WidgetPropertyType("color", "fontColor",
			"ff000000"));

		WidgetType listViewType = new WidgetType("ListView", widgetType);
		listViewType.addProperty(new WidgetPropertyType("float", "rowHeight",
			"20"));

		WidgetType listViewItemType = new WidgetType("ListViewItem", widgetType);
		listViewItemType.addProperty(new WidgetPropertyType("string", "text",
			"test"));
		listViewItemType.addProperty(new WidgetPropertyType("resource", "icon",
			"0"));

		WidgetType searchBarType = new WidgetType("SearchBar", widgetType);
		searchBarType.addProperty(new WidgetPropertyType("string", "text", ""));
		searchBarType.addProperty(new WidgetPropertyType("string",
			"placeholder", ""));
		searchBarType.addProperty(new WidgetPropertyType("bool",
			"showKeyboard", "false"));

		WidgetType navBarType = new WidgetType("NavBar", widgetType);
		navBarType.addProperty(new WidgetPropertyType("string", "title", ""));
		navBarType.addProperty(new WidgetPropertyType("string",
			"backButtonTitle", ""));

		WidgetType relativeLayoutType = new WidgetType("RelativeLayout",
			widgetType);

		WidgetType horizontalLayoutType = new WidgetType("HorizontalLayout",
			widgetType);
		horizontalLayoutType.addProperty(new WidgetPropertyType("int",
			"leftMargin", "0"));
		horizontalLayoutType.addProperty(new WidgetPropertyType("int",
			"topMargin", "0"));
		horizontalLayoutType.addProperty(new WidgetPropertyType("int",
			"bottomMargin", "0"));
		horizontalLayoutType.addProperty(new WidgetPropertyType("int",
			"rightMargin", "0"));
		horizontalLayoutType.addProperty(new WidgetPropertyType("int",
			"spacing", "0"));

		WidgetType verticalLayoutType = new WidgetType("VerticalLayout",
			widgetType);
		verticalLayoutType.addProperty(new WidgetPropertyType("int",
			"leftMargin", "0"));
		verticalLayoutType.addProperty(new WidgetPropertyType("int",
			"topMargin", "0"));
		verticalLayoutType.addProperty(new WidgetPropertyType("int",
			"bottomMargin", "0"));
		verticalLayoutType.addProperty(new WidgetPropertyType("int",
			"rightMargin", "0"));
		verticalLayoutType.addProperty(new WidgetPropertyType("int", "spacing",
			"0"));

		WidgetType glViewType = new WidgetType("GLView", widgetType);

		WidgetType webViewType = new WidgetType("WebView", widgetType);
		webViewType.addProperty(new WidgetPropertyType("string", "url",
			"http://www.mosync.com"));

		addType(screenType);
		addType(tabScreenType);
		addType(navScreenType);
		addType(buttonType);
		addType(imageButtonType);
		addType(imageType);
		addType(labelType);
		addType(editboxType);
		addType(listViewType);
		addType(listViewItemType);
		addType(searchBarType);
		addType(navBarType);
		addType(relativeLayoutType);
		addType(horizontalLayoutType);
		addType(verticalLayoutType);
		addType(glViewType);
		addType(webViewType);
	}

	public static WidgetType getTypeByName(String name)
	{
		return mTypes.get(name);
	}

	public String toString()
	{
		return mName;
	}

	private String mName;
	private ArrayList<WidgetPropertyType> mPropertyTypes;
	// UNUSED: private Component component;
	// UNUSED: private WidgetType base;

	private static Map<String, WidgetType> mTypes = new TreeMap<String, WidgetType>();
}
